#include "FormDataParser_c_api.h"
#include "FormDataParser.h"
#include <memory.h>
#include <string>
#include <fstream>

#ifdef __cplusplus
extern "C" {
#endif


std::string get_boundary(char* data)
{
    int len = strlen(data);
    std::string boundary;

    int pos = 0;
    for(int i = 0; i < len; i++)
    {
        if(data[i] == '\n' || data[i] == '\r')
        {
            pos = i;
            break;
        }
    }

    if(pos != 0)
    {
        std::unique_ptr<char[]> buf(new char[pos + 1]);
        memset(buf.get(), 0, pos + 1);
        memcpy(buf.get(), data, pos);
        boundary = std::string(buf.get());
    }

    return boundary;
}

FormNoda* parser_form_data(char* data, int dataSize, int* num)
{
    if(data == NULL) return NULL;

    std::string mboundary = get_boundary(data);
    if(mboundary.empty())
    {
        printf("file:%s function:%s line:%d, get boundary fail!\n", __FILE__, __FUNCTION__, __LINE__);
        return NULL;
    }
    printf("boundary:%s\n", mboundary.c_str());

    std::shared_ptr<std::string> sptr_data(new std::string(data));
    FormDataParser fdp(sptr_data, 0, mboundary);
    auto p = fdp.parse();

    int size = p.get()->size();
    *num = size;
    FormNoda* formNodas = (FormNoda*)malloc(size * sizeof(FormNoda));
    memset(formNodas, 0, size * sizeof(FormNoda));

    printf("file:%s function:%s line:%d part size : %d\n", __FILE__, __FUNCTION__, __LINE__, size);

    int i = 0 ;
    for (std::vector<FormItem>::iterator it = p->begin(); it != p->end();++it) 
    {
        formNodas[i].mNameLen = (*(it)).getName().size() + 1;
        formNodas[i].mName = (char*)malloc(formNodas[i].mNameLen);
        memset(formNodas[i].mName, 0, formNodas[i].mNameLen);
        memcpy(formNodas[i].mName, (*(it)).getName().c_str(), (*(it)).getName().size());

        if( (*(it)).isFile())
        {
            //是文件
            formNodas[i].isFile = 1;

            //formNodas[i].mContentLen = (*(it)).getContent().size();
            //formNodas[i].mContent = (char*)malloc(formNodas[i].mContentLen);
            //memset(formNodas[i].mContent, 0, formNodas[i].mContentLen);
            //memcpy(formNodas[i].mContent, (*(it)).getContent().c_str(), (*(it)).getContent().size());

            formNodas[i].mFileNameLen = (*(it)).getFileName().size()+1;
            formNodas[i].mFileName = (char*)malloc(formNodas[i].mFileNameLen);
            memset(formNodas[i].mFileName, 0, formNodas[i].mFileNameLen);
            memcpy(formNodas[i].mFileName, (*(it)).getFileName().c_str(), (*(it)).getFileName().size());

            int startPos = (*(it)).getDataStart();
            printf("file total size:%d   file start pos:%d\n", dataSize, startPos);
            formNodas[i].mContentLen = dataSize - startPos;
            formNodas[i].mContent = (char*)malloc(formNodas[i].mContentLen);
            memset(formNodas[i].mContent, 0, formNodas[i].mContentLen);
            memcpy(formNodas[i].mContent, data + startPos, formNodas[i].mContentLen);

           
            //std::fstream ofs("out.png", std::ios::out | std::ios::binary );
            //ofs.write(data + startPos, dataSize - startPos);
        }
        else
        {
            formNodas[i].isFile = 0;

            formNodas[i].mContentLen = (*(it)).getContent().size() + 1;
            formNodas[i].mContent = (char*)malloc(formNodas[i].mContentLen);
            memset(formNodas[i].mContent, 0, formNodas[i].mContentLen);
            memcpy(formNodas[i].mContent, (*(it)).getContent().c_str(), (*(it)).getContent().size());
        }
        
        
		//std::cout << (*(it)).getName() << ":" << (*(it)).getContent() <<std::endl;
        i++;
	}

    return formNodas;
}



void free_form_data(FormNoda* formNodas, int num)
{
    if(formNodas == NULL) return;

    for(int i = 0; i < num; i++)
    {
        if(formNodas[i].mName != NULL)
        {
            free(formNodas[i].mName);
            formNodas[i].mName = NULL;
        }

        if(formNodas[i].mContent != NULL)
        {
            free(formNodas[i].mContent);
            formNodas[i].mContent = NULL;
        }

    }

    free(formNodas);
}


#ifdef __cplusplus
}
#endif